package com.ebt.m.apps;

import android.content.Context;

import com.ebt.m.AppInitService;
import com.ebt.m.apps.response.ModulesBean;
import com.ebt.m.apps.response.ResponseModule;
import com.ebt.m.commons.buscomponent.listview.BaseListContract;
import com.ebt.m.commons.buscomponent.listview.BaseListPresenter;
import com.ebt.m.commons.buscomponent.listview.SectionItem;
import com.ebt.m.commons.model.data.BaseDataResult;
import com.ebt.m.commons.utils.NetUtils;
import com.ebt.m.commons.utils.RxUtil;
import com.ebt.m.data.rxModel.api.EBTAPI;
import com.ebt.m.data.rxModel.apibean.ModuleCount;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by Damon on 2017/10/26.
 * 应用数据获取页面
 */

public class AppsPresenter extends BaseListPresenter<BaseListContract.IView> {
    private List<SectionItem> items = new ArrayList<>();

    public AppsPresenter(Context context, BaseListContract.IView iView) {
        super(context, iView);
    }

    @Override
    public void loadNew(Object... args) {
        //请求接口加载模块数据
        EBTAPI ebtapi = AppInitService.getEbtApi();
        ebtapi.getModules1().compose(RxUtil.rxTransformer(iView))
                .doOnNext(result -> {
                    items.clear();
                    ResponseModule responseModule = result.getData();
                    List<ModulesBean> list = responseModule.getModules();
                    List<ModulesBean> listZhanye = new ArrayList<>();
                    List<ModulesBean> listService = new ArrayList<>();
                    List<ModulesBean> listJunbaoge = new ArrayList<>();
                    if (list != null && list.size() > 0) {
                        for (int i = 0; i < list.size(); i++) {
                            listService.clear();
                            listZhanye.clear();
                            listJunbaoge.clear();
                            //根据状态进行将数据添加到相应数组
                            for (ModulesBean module : list) {
                                if (module.getModuleType() == AppsView.APPS_VIEW_TYPE) {
                                    listZhanye.add(module);
                                } else if (module.getModuleType() == AppsView.SERVICE_VIEW_TYPE) {
                                    listService.add(module);
                                } else if (module.getModuleType() == AppsView.JUNBAOGE_VIEW_TYPE) {
                                    listJunbaoge.add(module);
                                }
                            }
                        }
                        if (listZhanye.size() > 0) {
                            SectionItem sectionItem = new SectionItem(AppsView.TITLE_VIEW_YTPE, "展业应用");
                            items.add(sectionItem);
                            for (ModulesBean moduleBean : listZhanye) {
                                SectionItem sectionItem1 = new SectionItem(AppsView.APPS_VIEW_TYPE, moduleBean);
                                items.add(sectionItem1);
                            }

                        }
                        if (listService.size() > 0) {
                            SectionItem sectionItem = new SectionItem(AppsView.TITLE_VIEW_YTPE, "服务查询");
                            items.add(sectionItem);
                            for (ModulesBean moduleBean : listService) {
                                SectionItem sectionItem1 = new SectionItem(AppsView.SERVICE_VIEW_TYPE, moduleBean);
                                items.add(sectionItem1);
                            }
                        }
                        if (listJunbaoge.size() > 0) {
                            SectionItem sectionItem = new SectionItem(AppsView.TITLE_VIEW_YTPE, "招募工具");
                            items.add(sectionItem);
                            for (ModulesBean moduleBean : listJunbaoge) {
                                SectionItem sectionItem1 = new SectionItem(AppsView.JUNBAOGE_VIEW_TYPE, moduleBean);
                                items.add(sectionItem1);
                            }
                        }
                    }
                })
                .subscribe(sectionItems -> {
                            iView.updateData(items);
                            getItemCount();
                        }
                        , throwable -> {
                            NetUtils.checkHttpException(mContext, throwable);
                            iView.errorData();
                        });
    }

    private void getItemCount() {
        EBTAPI ebtapi = AppInitService.getEbtApi();
        ebtapi.getModuleCount().compose(RxUtil.rxTransformer(iView))
                .subscribe(new Observer<BaseDataResult<List<ModuleCount>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseDataResult<List<ModuleCount>> moduleCountBaseDataResult) {
                        if (moduleCountBaseDataResult == null || moduleCountBaseDataResult.getData() == null) {
                            return;
                        }
                        ModuleCount moduleCount = null;
                        for (int i = 0; i < moduleCountBaseDataResult.getData().size(); i++) {
                            moduleCount = moduleCountBaseDataResult.getData().get(i);
                            SectionItem item = null;
                            for (int j = 0; j < items.size(); j++) {
                                item = items.get(j);
                                if (item.data instanceof ModulesBean) {
                                    ModulesBean modulesBean = (ModulesBean) item.data;
                                    int id = modulesBean.getId();
                                    if (id == moduleCount.getModuleId()) {
                                        ((ModulesBean) item.data).setCount(moduleCount.getCount());
                                    }
                                }
                            }
                            iView.updateData(items);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void refresh() {
        loadNew();
    }

    @Override
    public void loadNext(Object... args) {
    }

    @Override
    public List<SectionItem> createSectionItem(List datas) {
        return null;
    }
}
